package com.groot.medical.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.groot.common.controller.BaseServletController;
import com.groot.medical.entity.AdministrativeArea;
import com.groot.medical.entity.Menu;
import com.groot.medical.entity.PageBean;
import com.groot.medical.entity.RuralInstitution;
import com.groot.medical.entity.Ztree;
import com.groot.medical.service.AdministrativeAreaService;
import com.groot.medical.service.AdministrativeAreaServiceImpl;
import com.groot.medical.service.InstitutionService;
import com.groot.medical.service.InstitutionServiceImpl;

@WebServlet("/sys/institution.do")
public class RuralInstitutionController extends BaseServletController {
	private static final long serialVersionUID = 1L;
	private static Logger LOGGER = Logger.getLogger(LoginController.class);
	private InstitutionService institutionservice = new InstitutionServiceImpl();
	private AdministrativeAreaService areaService = new AdministrativeAreaServiceImpl();
	public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LOGGER.info("this is @WebServlet(\"/index.do\").execute()");
		return null;
	}
	/**
	 * 分页查询农合机构信息
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findInstitutionByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String tgId = request.getParameter("tgId");
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		List<Menu> menus=(List<Menu>) request.getSession().getAttribute("menuList");
		List<Menu> result = new ArrayList<Menu>();
		/*根据页面传过来的id与该用户所有权限的上一级id比较
		 * 若该权限的上一级传过来的id相同则存入新集合
		 */
		for(Menu menu: menus){
			if(menu.getMenuPid().equals(tgId)){
				result.add(menu);
			}
		}
		Gson gson = new Gson();
		request.setAttribute("resultMenus", gson.toJson(result));
		request.setAttribute("tgId", tgId);
		int pageSize = 5;
		PageBean areaPage = institutionservice.findByPage(pageNum,pageSize);
		System.out.println("areaPage:"+areaPage);
		request.setAttribute("areaPage", areaPage);
		return "/WEB-INF/page/institution/institution_list.jsp";
		//midecal/src/main/webapp/WEB-INF/page/institution/institution_list.jsp
	}
	/**
	 * 打开农合经办点页面
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String openAddInstitution(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("tgId",request.getParameter("tgId"));
		//查询行政地区信息形成树信息
		/*List<AdministrativeArea> areaList = areaService.findAllAreaInfo();*/
		//查询镇县两级所有行政区域信息
		List<AdministrativeArea> areaList = areaService.findOtherAreaInfo();
		//逐个遍历封装进ztree
		List<Ztree> ztrees = new ArrayList<>();
		for(int i=0;i<areaList.size();i++){
			AdministrativeArea area = areaList.get(i);
			Ztree ztree = new Ztree();
			ztree.setId(String.valueOf(area.getId()));
			ztree.setpId(String.valueOf(area.getPid()));
			ztree.setName(area.getAreaName());
			ztrees.add(ztree);
		}
		Gson gson = new Gson();
		String json = gson.toJson(ztrees);
		request.setAttribute("areaList", json);
		
		return "/WEB-INF/page/institution/institution_add.jsp";
	}
	/**
	 * 添加农合经点信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addInstitutionInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String pid = request.getParameter("pid");
		String institutionid = request.getParameter("institutionid");
		String institutionname = request.getParameter("institutionname");
		String institutionaddress = request.getParameter("institutionaddress");
		String tgId = request.getParameter("tgId");
		String pageNum = request.getParameter("pageNum");
		RuralInstitution institution = new RuralInstitution();
		institution.setInstitutionId(institutionid);
		institution.setInstitutionName(institutionname);
		institution.setInstitutionAddress(institutionaddress);
		
		//添加农合经办点信息
		int i = institutionservice.addInstitutionInfo(institution,pid);
		if(i==1){
			request.setAttribute("msg", "<script>alert('修改成功')</script>");
			return findInstitutionByPage(request,response);
		
		}else
			LOGGER.info("添加失败"+i);
		return null;
	}
	/**
	 * 打开修改农合机构信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String openEditInstitution(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("tgId",request.getParameter("tgId"));
		String id = request.getParameter("id");
		RuralInstitution institution=institutionservice.findInstitutionInfoById(id);
		request.setAttribute("institution", institution);
		return "/WEB-INF/page/institution/institution_edit.jsp";
	}
	/**
	 * 修改农合机构信息
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String editInstitutionInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("tgId",request.getParameter("tgId"));
		String institutionLevel = request.getParameter("institutionLevel");
		String id = request.getParameter("id");
		String areacode = request.getParameter("areacode");
		String institutionId = request.getParameter("institutionId");
		String institutionName = request.getParameter("institutionName");
		String institutionAddress = request.getParameter("institutionAddress");
		RuralInstitution institution = new RuralInstitution();
		institution.setId(Integer.parseInt(id));
		institution.setAreacode(areacode);
		institution.setInstitutionAddress(institutionAddress);
		institution.setInstitutionId(institutionId);
		institution.setInstitutionLevel(institutionLevel);
		institution.setInstitutionName(institutionName);
		
		institutionservice.editInstitutionInfo(institution);
		return findInstitutionByPage(request,response);
	}
}
